#include "StdAfx.h"
#include "TinyTalkObjectPool.h"
#include "TalkObject.h"
#include "VoiceTalkObject.h"
#include "TinyServerDefine.h"
#include "TinyTalkState.h"
namespace TalkProcess{

	CTinyTalkObjectPool::CTinyTalkObjectPool(void)
	{
	}

	CTinyTalkObjectPool::~CTinyTalkObjectPool(void)
	{
	}

	CTalkObject* CTinyTalkObjectPool::GetTalkObjectByDeviceID(std::string& deviceID)
	{
		for( map<std::string, CTalkObject*>::iterator it = m_TalkObjects.begin();
			it !=  m_TalkObjects.end(); it++ )
		{
			CVoiceTalkObject* tmpTalk = dynamic_cast<CVoiceTalkObject*>(it->second);
			if(tmpTalk->calledID == deviceID)
				return it->second;
		}
		return NULL;
	}

	CVoiceTalkObject* CTinyTalkObjectPool::GetNewVoiceTalkObject()
	{
		CVoiceTalkObject* talk = NULL; 
		//init

		talk = new CVoiceTalkObject();
		talk->changeState(VoiceTalk_State_Idle);
		m_TalkObjects.insert(std::pair<std::string,CTalkObject*>(talk->GetTalkID(),talk));
		return talk;
	}
	void CTinyTalkObjectPool::ProcessReceivedMsg(CTinyMsg& inMsg,CTinyMsg& OutMsg)
	{
		//if alert,first use pre route
		/*if(inMsg.getMsgIndex() == VOICECARD_ACTION&&inMsg.getMsgItem()==VOICECARD_ALERT)
		{
		g_PreRouteEngine.ProcessCall(inMsg,OutMsg);
		}*/

		if(inMsg.getMsgIndex() == VOICECARD_ACTION)
		{
			switch (inMsg.getMsgItem())
			{
			case VOICECARD_ALERT:
				{
					string moudleType,deviceID,callerID,deviceType;
					inMsg.getDefaultParamByIndex(VOICECARD_ALERT_MODULENAME,moudleType);
					inMsg.getDefaultParamByIndex(VOICECARD_ALERT_DEVICEID,deviceID);
					inMsg.getDefaultParamByIndex(VOICECARD_ALERT_DEVICETYPE,deviceType);

					if(GetTalkObjectByDeviceID(deviceID) == NULL)
					{
						//create talk object
						CVoiceTalkObject* talk = GetNewVoiceTalkObject();
						talk->calledID = deviceID;
						//must record call arrived time to db
						talk->ProcessReceivedMsg(inMsg,OutMsg);
					}
					//if ivr,pickup
					if(deviceType == "Trunk")
					{
						OutMsg.setMsgIndex(VOICECARD_ACTION);
						OutMsg.setMsgItem(VOICECARD_PICKUP);
						OutMsg.setDefaultParam(VOICECARD_PICKUP_MODULENAME,moudleType);
						OutMsg.setDefaultParam(VOICECARD_PICKUP_DEVICEID,deviceID);
						OutMsg.setDefaultParam(VOICECARD_PICKUP_DEVICETYPE,deviceType);
					}
					//if agent or others,just wait

				}break;
			case VOICECARD_CONNECTED:
				{
					string moudleType,deviceID,callerID,deviceType;
					inMsg.getDefaultParamByIndex(VOICECARD_CONNECTED_MODULENAME,moudleType);
					inMsg.getDefaultParamByIndex(VOICECARD_CONNECTED_DEVICEID,deviceID);
					inMsg.getDefaultParamByIndex(VOICECARD_CONNECTED_DEVICETYPE,deviceType);

					//if ivr,change to connected state,start ivr flow
					//get talk object first.
					//g_DevicePool.GetDeviceID(VOICECARD_MODULENAME,)
					CTalkObject* talk = GetTalkObjectByDeviceID(deviceID);
					if(talk = NULL)
					{
						g_Logger.log(debug1,"can not found talkobject for device"+deviceID);
					}
					else
					{
						talk->changeState(VoiceTalk_State_Connected);
						talk->ProcessReceivedMsg(inMsg,OutMsg);
					}

				}break;
			default:
				{
					string deviceID;
					inMsg.getDefaultParamByIndex(0,deviceID);
					GetTalkObjectByDeviceID(deviceID)->ProcessReceivedMsg(inMsg,OutMsg);
				}
				break;
			}
		}
		/*if(inMsg.getMsgIndex() == PBX_ACTION)
		{
		}*/

	}
}